package com.joinlabs.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.joinlabs.entity.User;
import com.joinlabs.entity.Weibo;
import com.joinlabs.service.IForwardService;
import com.joinlabs.service.ILogService;
import com.joinlabs.service.ITopicService;
import com.joinlabs.service.IUserService;
import com.joinlabs.service.IWeiboService;

@Controller
@RequestMapping("/common")
@SessionAttributes(names= {"user","fleg","gAndf"})
public class TaDeHomePageAction {
	@Resource
	private IUserService userService;//用户
	@Resource
	private ITopicService topicService;//话题
	@Resource
	private IWeiboService weiboService;//微博
	@Resource
	private IForwardService forwardService;//转发
	@Resource
	private ILogService logService;//日志
	
	
	
	@RequestMapping("/showTaDeHomePage")
	public String showTaDeHomePage(ModelMap map,long userId ) {
		//模拟一个登录成功的用户
				User user1 = userService.selectByPrimaryKey(userId);
				User user = (User) map.get("user");		
				//添加到session域中
				List<User> GuangZuState = userService.selectGuangZuStateById((long)user.getUserId(),(long)user1.getUserId());
				if( GuangZuState.size()<1) {
					String GZState = "0";
					map.addAttribute("GZState", GZState);
					map.addAttribute("user", user);
					map.addAttribute("user1", user1);
					return "/GeRenHomePage/TaDeHomePage";
				}
				String GZState = "1";
				map.addAttribute("GZState", GZState);
				map.addAttribute("user1", user1);
				map.addAttribute("user", user);
				//添加到requst域中
				map.addAttribute("user1", user1);
		return "/GeRenHomePage/TaDeHomePage";
	}
	
	@RequestMapping("/showTaDeHomePage2")
	public String showTaDeHomePage2(ModelMap map, String fleg, int userid,int userid2) {
//		System.out.println(fleg);
		
		
		System.out.println("我是主页方法2的userid:"+userid);
		System.out.println(userid2);
		System.out.println("我是主页方法2的fleg:"+fleg);
		//模拟一个登录成功的用户
		User user1 = userService.selectByPrimaryKey((long)userid);
		User user2 = userService.selectByPrimaryKey((long)userid2);			
		
		
		//添加到requst域中
		map.addAttribute("user1", user2);
		//添加到session域中
		map.addAttribute("fleg", fleg);	
		map.addAttribute("user", user1);
		return "redirect:/common/showTaDeHomePage?userId="+user2.getUserId();
	}
	
	@RequestMapping("/showTaDeHomePage3")
	public String showTaDeHomePage3(ModelMap map,long userId) {
		//模拟一个登录成功的用户
				User user1 = userService.selectByPrimaryKey(userId);	
				String GZState = "0";
				map.addAttribute("GZState", GZState);
				
				map.addAttribute("user1", user1);
				return "/GeRenHomePage/TaDeHomePage2";
	}
	
	@RequestMapping("/showTaDeHomePage4")
	public String showTaDeHomePage4(ModelMap map, String fleg, int userid) {
//		System.out.println(fleg);
		map.addAttribute("fleg", fleg);	
		
		System.out.println("我是主页方法2的userid:"+userid);

		System.out.println("我是主页方法2的fleg:"+fleg);
		//模拟一个登录成功的用户
		User user1 = userService.selectByPrimaryKey((long)userid);		

		//添加到requst域中
		map.addAttribute("user2", user1);

		return "redirect:/common/showTaDeHomePage3";
	}
	
	
	
	
	
	@RequestMapping("/addAttention")
	@ResponseBody
	public String addAttention(ModelMap map, int userid,int userid2) {

		System.out.println("我是主页方法2的userid:"+userid);
		System.out.println(userid2);

		User user1 = userService.selectByPrimaryKey((long)userid);
		User user2 = userService.selectByPrimaryKey((long)userid2);	
		long num = user2.getUserConcernnum();
		user2.setUserConcernnum(num+1);
		num = user2.getUserFans();
		user1.setUserFans(num+1);
		
		int rs = userService.serviceAddAttention((long)userid,(long)userid2);
		userService.updateUserService(user2);
		userService.updateUserService(user1);
		
		if(rs>0) {
			String GZState = "1";
			map.addAttribute("GZState", GZState);
			//添加到requst域中
			map.addAttribute("user2", user2);
			//添加到session域中
			map.addAttribute("user", user1);
			
			return "success";
		}
		
		return "er";
	}
	
	
	@RequestMapping("/deleteAttention")
	@ResponseBody
	public String deleteAttention(ModelMap map, int userid,int userid2) {

		System.out.println("我是主页方法2的userid:"+userid);
		System.out.println(userid2);

		User user1 = userService.selectByPrimaryKey((long)userid);
		User user2 = userService.selectByPrimaryKey((long)userid2);	
		
		long num = user2.getUserConcernnum();
		user2.setUserConcernnum(num-1);
		num = user2.getUserFans();
		user1.setUserFans(num-1);
		
		int rs = userService.serviceDeleteAttention((long)userid,(long)userid2);
		userService.updateUserService(user2);
		userService.updateUserService(user1);
		
		
		if(rs>0) {
			String GZState = "0";
			map.addAttribute("GZState", GZState);
			//添加到requst域中
			map.addAttribute("user2", user2);
			//添加到session域中
			map.addAttribute("user", user1);
			
			return "success";
		}
		
		return "er";
	}	
	
	
	
	
	
	
	
	/*********       第二层Load界面               下    *********/		
	@RequestMapping("/showTaDeHomePageGuZhu")
	public String showTaDeHomePageGuZhu(ModelMap map, int userid,int userid2) {
		//查询当前查询的人的信息
		User user1 = userService.selectByPrimaryKey((long)userid);
		User user2 = userService.selectByPrimaryKey((long)userid2);
		
		
		System.out.println("我是关注方法的"+userid);
		System.out.println(userid2);
		//查询当前的人我的共同关注
		List<User> GongTongGuangZu = userService.selectGongTongById((long)userid,(long)userid2);
		
		//查询我关注的人也关注他
		List<User> WoGuangZu = userService.selectWoGuangZuDeById((long)userid,(long)userid2);
		//查出全部微博
		List<Weibo> GeRenweiboList = weiboService.selectWeiboById((long)userid);
		
		
		//添加到session域中
		map.addAttribute("user", user2);
		
		//添加到requst域中
		map.addAttribute("user1", user1);
		map.addAttribute("WoGuangZu", WoGuangZu);
		map.addAttribute("GongTongGuangZu", GongTongGuangZu);
		map.addAttribute("GeRenweiboList", GeRenweiboList);
		
		
		return "/GeRenHomePage/TaDeHomePage_GuanZu";
	}
	
	@RequestMapping("/showTaDeHomePageGuZhu2")
	public String showTaDeHomePageGuZhu2(ModelMap map, int userid) {
		//查询当前查询的人的信息
		User user1 = userService.selectByPrimaryKey((long)userid);
		
		System.out.println("我是关注方法的"+userid);

		//查出全部微博
		List<Weibo> GeRenweiboList = weiboService.selectWeiboById((long)userid);

		//添加到requst域中
		map.addAttribute("user1", user1);
		map.addAttribute("GeRenweiboList", GeRenweiboList);
		
		
		return "/GeRenHomePage/TaDeHomePage_GuanZu2";
	}
	
	
	@RequestMapping("/showTaDeHomePageFenSi")
	public String showTaDeHomePageFenSi(ModelMap map, int userid,int userid2) {
		String fleg;
		fleg=(String) map.get("fleg");
		
		
		String gAndf = fleg;
		fleg = "0";
		User user1 = userService.selectByPrimaryKey((long)userid);
		User user2 = userService.selectByPrimaryKey((long)userid2);
		
		
		System.out.println("我是粉丝方法的"+userid);
		System.out.println(userid2);
		map.addAttribute("user1", user1);
		map.addAttribute("user", user2);
		
		
		map.addAttribute("fleg", fleg);
//		System.out.println(map.get("fleg"));
		map.addAttribute("gAndf", gAndf);
		
		
		
		
		return "/GeRenHomePage/TaDeHomePage_Fenshi";
	}
	

	
/*********       第二层Load界面               上     *********/		
/*********       第三层Load界面               下     *********/	
	@RequestMapping("/showGuanZuKuang1")
	public String showGuanZuKuang1(ModelMap map, int userid) {
//		System.out.println(userid);
		
		List<User> GZ = userService.selectGZById((long)userid);
		map.addAttribute("GZ", GZ);
		
		return "/GeRenHomePage/HomePage_GuZuKuang";
	}
	@RequestMapping("/showGuanZuKuang4")
	public String showGuanZuKuang4(ModelMap map, int userid,int userid2) {
//		System.out.println(userid);
		
		List<User> GZ = userService.selectGZById2((long)userid, userid2);
		map.addAttribute("GZ", GZ);
		
		return "/GeRenHomePage/HomePage_GuZuKuang";
	}
	@RequestMapping("/showGuanZuKuang2")
	public String showGuanZuKuang2(ModelMap map, int userid,int userid2) {
		//查询当前的人我的共同关注
		List<User> GongTongGuangZu = userService.selectGongTongById((long)userid,(long)userid2);
		map.addAttribute("GZ", GongTongGuangZu);	

		return "/GeRenHomePage/HomePage_GuZuKuang";
	}
	@RequestMapping("/showGuanZuKuang3")
	public String showGuanZuKuang3(ModelMap map, int userid,int userid2) {
		//查询我关注的人也关注他
		List<User> WoGuangZu = userService.selectWoGuangZuDeById((long)userid,(long)userid2);
		map.addAttribute("GZ", WoGuangZu);
		
		
		return "/GeRenHomePage/HomePage_GuZuKuang";
	}
	@RequestMapping("/showTaIn")
	public String showTaDeEditInfo(ModelMap map, long userid) {
		//查询当前查询的人的信息
		User user1 = userService.selectByPrimaryKey((long)userid);
		map.addAttribute("user1", user1);
		return "/GeRenHomePage/TaDeEditInfo";
	}
	
	
	@RequestMapping("/showShuoshuokuang")
	public String showShuoshuokuang(ModelMap map, int userid) {
		System.out.println("*****************"+userid);
		//模拟一个登录成功的用户
		User user = userService.selectByPrimaryKey((long) userid);
			
		//添加到session域中
		map.addAttribute("user", user);
		//查出全部微博
		List<Weibo> GeRenweiboList = weiboService.selectWeiboById(user.getUserId());
		map.addAttribute("GeRenweiboList", GeRenweiboList);

		return "/GeRenHomePage/HomePage_main_shuoshuokuang";
	}
	
	

	@RequestMapping("/showFansKuang")
	public String showFansKuang(ModelMap map, int userid) {
		
		
		List<User> Fans = userService.selectFansById((long)userid);
		map.addAttribute("Fans", Fans);
		System.out.println(Fans);
		return "/GeRenHomePage/HomePage_fenshiKuang";
	}
	/*********       第三层Load界面               上     *********/	
}
